package com.offcn.controller;


import com.offcn.pojo.Member;
import com.offcn.pojo.vo.LoginUserVo;
import com.offcn.service.LoginUserService;
import com.offcn.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    LoginUserService loginUserService;
    @Autowired
    MemberService memberService;
//       细节：
//          当一个方法处理两个请求的时候，一定要做区分
//          当访问http://localhost:80/eastCMS/login
//          我们是为了跳转到login.jsp
//          结果  loginUserVo 内容为null
//          所以会出空指针异常
//          于是乎我们想到，将两种请求分离
//
//       需求：展示登录页面
//       URL： http://localhost:80/eastCMS/login
//       逻辑：1：接收请求
//             2：跳转到登录页面


    //       需求：登录主页面
//       URL：http://localhost:80/eastCMS/login?userName=admin&password=admin
//       逻辑：
//           1：接收请求
//           2：接收数据
//           3：进行验证
//               3.1 如果有此人
//                     3.1.0 查询出当前登录人员，并且存储到session中！！！
//                    3.1.1 跳转到index.jsp
//               3.2 如果没有此人
//                    跳转到login.jsp

    @RequestMapping("login")
    public String login(LoginUserVo loginUserVo, HttpServletRequest request) {

        if (loginUserVo.getUserName() != "" && loginUserVo.getPassword() != null && loginUserVo.getPassword() != "" && loginUserVo.getUserName() != null) {
            HttpSession session = request.getSession();
            boolean flag = loginUserService.loginUserByLoginUserVo(loginUserVo);
            if (flag) {

                Member member = (Member) memberService.selectMemberByLoginUserVo(loginUserVo);
                session.setAttribute("member", member);
                System.out.println("============");
                System.out.println("member=" + member);
                return "index";
            } else {
                return "login";
            }

        } else {
            return "login";
        }

    }

    /*@RequestMapping("login")
    public String login(LoginUserVo loginUserVo, HttpServletRequest request) {

        if (loginUserVo.getUserName() != "" && loginUserVo.getUserName() != null && loginUserVo.getPassword() != "" && loginUserVo.getPassword() != null) {
            HttpSession session = request.getSession();
            boolean flag = loginUserService.selectLoginUserByLoginUserVo(loginUserVo);
            if (flag) {
                Member member = (Member) memberService.selectMemberByLoginUserVo(loginUserVo);
                System.out.println("login----" + member);
                session.setAttribute("member", member);
                
                return "index";
            } else {
                return "login";
            }
        } else {
            return "login";
        }

    }*/


}
